<?php
/**
 * url统一生成帮助函数库
 */

/**
 * 应用的图片地址
 * @param string $fn 文件名 like $app['icon_small']
 * @param bool $print 是否将地址输出
 * @return string
 */
function appImageUrl($fn, $print = FALSE)
{
    $url = Oa_Util_Config::getConfig('app_img_prefix').$fn;
    if ($print) {
        echo $url;
    }
    return $url;
}

/**
 * 玩应用页地址
 * 
 * @param int|array $appInfoOrId 应用信息数组或id
 * @param bool $print 是否将地址输出
 * @return string
 */
function appPlayUrl($appInfoOrId, $print = FALSE)
{
    $canvas_page_url = '';
    $appId = 0;
    if (is_array($appInfoOrId)) {
        $canvas_page_url = 
            isset($appInfoOrId['canvas_page_url']) ? $appInfoOrId['canvas_page_url'] : '';
        $appId = 
            isset($appInfoOrId['id']) ? $appInfoOrId['id'] : 0;
        $app = $appInfoOrId;
    } elseif (is_numeric($appInfoOrId)) {
        $app = self::getAppInfo($appInfoOrId);
        if ($app) {
            $canvas_page_url = $app['canvas_page_url'];
        }
        $appId = $appInfoOrId;
    }
    
    $url = '';
    $baseUrl = Oa_Util_Config::getConfig('apps_base_url');
    
    if ($app['app_type'] == OpAppModel::APP_TYPE_SYSTEM) {
        $url = $app['canvas_callback_url'];    
    } else if (!empty($canvas_page_url)) {
        $url = "{$baseUrl}/{$canvas_page_url}";
    } else if (!empty($appId)) {
        $url = "{$baseUrl}/play?id={$appId}";
    }
    
    if ($print) {
        echo $url;
    }
    
    return $url;
}

/**
 * 应用评论页地址
 * 
 * @param int|array $appInfoOrId 应用信息数组或id
 * @param bool $print
 * @return string
 */
function appCommentUrl($appInfoOrId, $print = FALSE) 
{
    $canvas_page_url = '';
    $appId = 0;
    if (is_array($appInfoOrId)) {
        $canvas_page_url = 
            isset($appInfoOrId['canvas_page_url']) ? $appInfoOrId['canvas_page_url'] : '';
        $appId = 
            isset($appInfoOrId['id']) ? $appInfoOrId['id'] : 0;
    } elseif (is_numeric($appInfoOrId)) {
        if ($app) {
            $canvas_page_url = $app['canvas_page_url'];
        }
        $appId = $appInfoOrId;
    }
    
    $url = '';
    $baseUrl = Oa_Util_Config::getConfig('apps_base_url');
    
    if (!empty($canvas_page_url)) {
        $url = "{$baseUrl}/{$canvas_page_url}/comment";
    } else if (!empty($appId)) {
        $url = "{$baseUrl}/comment?id={$appId}";
    }
    
    if ($print) {
        echo $url;
    }
    
    return $url;
}
